package com.dashu.park.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.park.dto.PageDTO;
import com.dashu.park.dto.company.EnterpriseDepartmentDTO;
import com.dashu.park.result.PageEnty;
import com.dashu.park.result.ResultBean;
import com.dashu.park.service.EnterpriseDepartmentService;
import com.dashu.park.vo.UpdateEntityVO;
import com.dashu.park.vo.company.EnterpriseDepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/22ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/EnterpriseDepartment")
@RestController
@Slf4j
@Api(value = "dashu/EnterpriseDepartment", tags = {"单位部门模块"}, description = "后台")
public class EnterpriseDepartmentController {

    @Autowired
    private EnterpriseDepartmentService enterpriseDepartmentService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addDepartment")
    @ApiOperation(value = "部门添加", notes = "部门添加")
    public ResultBean<EnterpriseDepartmentDTO> addDepartment(@RequestBody @Validated EnterpriseDepartmentVO enterpriseDepartmentVO) {
        EnterpriseDepartmentDTO enterpriseDepartmentDTO = enterpriseDepartmentService.existByCompanyDepartmentName(enterpriseDepartmentVO.getEnterpriseName(),enterpriseDepartmentVO.getEnterpriseId());
        if (enterpriseDepartmentDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY_DEPARTMENT.getCode(), ReplRyCode.EXIST_COMPANY_DEPARTMENT.getMessage(), null);
        }
        if (enterpriseDepartmentVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门添加", enterpriseDepartmentService.add(enterpriseDepartmentVO));
    }

    @PostMapping("/updataDepartment")
    @ApiOperation(value = "部门信息修改", notes = "部门信息修改")
    public ResultBean<EnterpriseDepartmentDTO> updataDepartment(@RequestBody @Validated UpdateEntityVO<EnterpriseDepartmentVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息修改", enterpriseDepartmentService.updataByEnterpriseDepartmentId(updateEntityVO));
    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询部门列表", notes = "父节点查询部门列表")
    public ResultBean<List<EnterpriseDepartmentDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询部门列表", enterpriseDepartmentService.findByCreteUser(creteUserVo));
    }

    @GetMapping("/findDepartment")
    @ApiOperation(value = "部门信息查询", notes = "部门信息查询")
    public ResultBean<EnterpriseDepartmentDTO> findDepartment(long enterpriseDepartmentId) {
        if (enterpriseDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息查询", enterpriseDepartmentService.findByEnterpriseDepartmentId(enterpriseDepartmentId));
    }

    @DeleteMapping("/deleteDepartment")
    @ApiOperation(value = "部门信息删除", notes = "部门信息删除")
    public ResultBean<Boolean> deleteDepartment(long enterpriseDepartmentId) {
        if (enterpriseDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息删除", enterpriseDepartmentService.deleteByEnterpriseDepartmentId(enterpriseDepartmentId));
    }

    @PostMapping("/pageDepartmentList")
    @ApiOperation(value = "部门信息列表", notes = "部门信息列表")
    public ResultBean<PageDTO<EnterpriseDepartmentDTO>> pageDepartmentList(@RequestBody @Validated PageEnty<EnterpriseDepartmentVO> departmentListPageEntyVO) throws Exception {
        PageDTO<EnterpriseDepartmentDTO> pageDTO = new PageDTO<>();
        if (departmentListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = enterpriseDepartmentService.departmentCount(departmentListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(enterpriseDepartmentService.pageDepartmentList(departmentListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息列表",pageDTO );
    }
}
